package Demo1;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class listServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取项目的根路径
        String path = req.getContextPath();

//        设置响应的数据类型.
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        // 获取数据库中的数据
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

 out.print("<!DOCTYPE html>");
 out.print("<html lang='en'>");
 out.print("<head>");
 out.print("   <meta charset='UTF-8'>");
 out.print("   <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
 out.print("   <title>List</title>");
 out.print("   <style>");
 out.print("               table{");
 out.print("           border: 2px solid gray;");
 out.print("           margin:30px auto;");
 out.print("           border-collapse: collapse;");
 out.print("           text-align: center;");
 out.print("           height: 150px;");
 out.print("       }");
 out.print("  tr,td,th{");
 out.print("        border: 2px solid gray;");
 out.print("    }");
 out.print(" </style>");

 out.print("<script>");
 out.print("               function del(dno){");
 out.print("           if(window.confirm('亲确定要删除吗?')){");
 out.print("               window.location.href = 'http://localhost:8080"+path+"/dept/delete?deptno='+dno; ");
 out.print("           }");
 out.print("       }");
 out.print("   </script>");

 out.print(" </head>");
 out.print(" <body>");
 out.print("");
 out.print("  <table width='50%' cellspacing='0' cellpadding='0'>");
 out.print("      <caption>部门信息表</caption>");
 out.print("      <thead>");
 out.print("          <tr>");
 out.print("              <th>部门号</th>");out.print("               <th>名称</th>");
 out.print("              <th>工作地点</th>");
 out.print("              <th>操作</th>");
 out.print("          </tr>");
 out.print("      </thead>");
 out.print("      <tbody>");




        try {

            connection = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                // 获取数据库的数据
                String deptno = resultSet.getString("deptno");
                String dname = resultSet.getString("dname");
                String loc = resultSet.getString("loc");

                out.print("          <tr>");
                out.print("             <td>"+deptno+"</td>");
                out.print("               <td>"+dname+"</td>");
                out.print("              <td>"+loc+"</td>");
                out.print("              <td>");
                out.print("                  <a href='javascript:void(0);' onclick='del("+deptno+");'>删除</a>");
                out.print("                   <a href='"+path+"/dept/edit?deptno="+deptno+"'>修改</a>");
                out.print("                   <a href='"+path+"/dept/detail?deptno="+deptno+"'>详情</a>");
                out.print("               </td>");
                out.print("           </tr>");


                System.out.println(deptno);
                System.out.println(dname);
                System.out.println(loc);

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            // 释放资源
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        out.print("       </tbody>");
        out.print("       <tfoot >");
        out.print("           <tr>");
        out.print("               <td colspan='4' style='font-size: 16px;'><a href='/oa2/add.html'>新增</a></td>");
        out.print("           </tr>");
        out.print("       </tfoot>");
        out.print("   </table>");
        out.print("</body>");
        out.print("</html>");
    }
}
